package cn.tedu.stx_admin.controller;

import cn.tedu.stx_admin.pojo.param.AdminAddNewParam;
import cn.tedu.stx_admin.pojo.param.AdminLoginParam;
import cn.tedu.stx_admin.pojo.vo.AdminLoginResultVO;
import cn.tedu.stx_admin.service.IAdminService;
import cn.tedu.stx_common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

import static cn.tedu.stx_common.consts.JwtClaimsConsts.CLAIM_USER_AGENT;

@Api(tags = "01.管理员")
@Slf4j
@RestController
@RequestMapping("/admin/")
public class AdminController {
    @Autowired
    private IAdminService service;

    public AdminController() {
        log.info("创建控制器对象：AdminController");
    }

    @ApiOperation(value = "添加admin")
    @PreAuthorize("hasAuthority('/admin/add-admin')")
    @ApiOperationSupport(order = 100)
    @PostMapping("add-admin")
    public JsonResult addAdmin(@Validated AdminAddNewParam param){
        log.info("adminAddNewParam: {}",param);
        service.addNew(param);
        return JsonResult.ok();
    }

    @ApiOperation(value = "管理员登录")
    @ApiOperationSupport(order = 200)
    @PostMapping("login")
    public JsonResult login(@Validated AdminLoginParam param, @ApiIgnore HttpServletRequest request){
        log.info("登录使用的账号：{}",param);
        String userAgent=request.getHeader("userAgent");
        String remoteAddr= request.getRemoteAddr();
        AdminLoginResultVO resultVO = service.login(param,userAgent,remoteAddr);
        return JsonResult.ok(resultVO);
    }

    @ApiOperation(value = "删除后台用户")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/admin/delete')")
    @PostMapping("delete")
    public JsonResult delete(@PathVariable @RequestParam Long id){
        log.info("开始执行删除功能："+id);
        service.delete(id);
        return JsonResult.ok();
    }

}
